﻿@model PersonalAccount_AlterEmailAddress
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_OridinaryPage.cshtml";
}

@section Header{
    <link rel="stylesheet" href="~/css/common.css" />
    <link rel="stylesheet" href="~/js/plugins/autoComplete/jquery.autocomplete.css" />
}

<section>
    <!--<h2>
        <small></small>
    </h2>-->
    <ol class="breadcrumb breadcrumb-pd clear-mgb bg-transparent">
        <li><a href="#"><i class="fa fa-dashboard"></i>个人域账号管理</a></li>
        <li class="active">邮件地址变更</li>
    </ol>
</section>
<!-- Main content -->
<section class="content-mini clear-pdt clear-pdb">
    <div class="row">
        <div class="col-xs-12">
            <div class="box clear-mgb">
                <!--<div class="box-header">
                    <h3 class="box-title"></h3>
                </div>-->
                <!-- /.box-header -->
                <div class="box-body">
                    <form class="form-horizontal" id="dataForm">
                        <input type="hidden" id="DeptId" name="DeptId" />
                        <input type="hidden" id="DeptName" name="DeptName" />
                        <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                        <input type="hidden" id="TaskId" name="TaskId" value="@ViewBag.TaskId" />
                        <input type="hidden" id="Action" name="Action" value="" />
                        <div class="form-group">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-xs-12">
                                        <div class="row">
                                            <div class="col-xs-3">
                                                <label for="SAMAccountName" class="col-xs-2 control-label  text-left pd-lft pd-rt">域账号</label>
                                                <div class="col-xs-10">
                                                    <input type="text" class="form-control userAC" id="SAMAccountName" name="SAMAccountName" placeholder="域账号" value="@(Model.SAMAccountName ?? SessionUtility.GetBasicUserInfo().UserCode)" onblur="getEmailAddress(this)">
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="OldEmailName" class="col-xs-2 control-label  text-left pd-lft pd-rt">原Email</label>
                                                <div class="col-xs-10">
                                                    <input type="text" class="form-control" id="OldEmailName" name="OldEmailName" placeholder="原Email" value="@(Model.OldEmailName ?? SessionUtility.GetBasicUserInfo().Email)" readonly="readonly">
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="NewEmailName" class="col-xs-2 control-label  text-left pd-lft pd-rt">新Email</label>
                                                <div class="col-xs-10 input-group" style="padding-right:15px;padding-left:15px;">
                                                    <input type="text" class="form-control" id="NewEmailName" name="NewEmailName" placeholder="新Email" onblur="checkEmailAddres()" maxlength="35" value="@Model.NewEmailName">
                                                    <span class="input-group-addon">@(Dapper_Core.Model.AppVariable.H3cEmailPostfix)</span>
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="FirstDeptCode" class="col-xs-2 control-label  text-left pd-lft pd-rt">一级部门</label>
                                                <div class="col-xs-10">
                                                    @Html.DDLDepartment1("FirstDeptCode", new { @id = "FirstDeptCode", @class = "form-control", @onchange = "setSecondDept()" }, null, 0)
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-xs-12">
                                        <div class="row">
                                            <div class="col-xs-3">
                                                <label for="SecondDeptCode" class="col-xs-2 control-label  text-left pd-lft pd-rt">二级部门</label>
                                                <div class="col-xs-10">
                                                    <select id="SecondDeptCode" name="SecondDeptCode" class="form-control" onchange="setThirdDept()"></select>
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="ThirdDeptCode" class="col-xs-2 control-label  text-left pd-lft pd-rt">三级部门</label>
                                                <div class="col-xs-10">
                                                    <select id="ThirdDeptCode" name="ThirdDeptCode" class="form-control" onchange="setManager()"></select>
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="DeptManager" class="col-xs-2 control-label  text-left pd-lft pd-rt">部门经理</label>
                                                <div class="col-xs-10">
                                                    <input type="text" class="form-control" id="DeptManager" name="DeptManager" placeholder="部门经理" readonly="readonly" value="@Model.DeptManager">
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="SimpleDisplayName" class="col-xs-2 control-label  text-left pd-lft pd-rt">对外显示名</label>
                                                <div class="col-xs-10">
                                                    <input type="text" class="form-control" id="SimpleDisplayName" name="SimpleDisplayName" placeholder="对外显示名" maxlength="30" value="@Model.SimpleDisplayName">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-xs-12">
                                        <div class="row">
                                            <div class="col-xs-3">
                                                <label for="ProxyMan" class="col-xs-2 control-label  text-left pd-lft pd-rt">代申请人</label>
                                                <div class="col-xs-10">
                                                    <input type="text" readonly="readonly" class="form-control" id="ProxyMan" name="ProxyMan" placeholder="代申请人" value="@(Model.ProxyMan ?? SessionUtility.GetBasicUserInfo().PinyinCode)">
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="CreateDate" class="col-xs-2 control-label  text-left pd-lft pd-rt">申请时间</label>
                                                <div class="col-xs-10">
                                                    <input type="text" readonly="readonly" class="form-control" id="CreateDate" name="CreateDate" placeholder="申请时间" value="@(Model.CreateDate)">
                                                </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <label for="Remarks" class="col-xs-2 control-label  text-left pd-lft pd-rt">原因</label>
                                                <div class="col-xs-10">
                                                    <input type="text" class="form-control" id="Remarks" name="Remarks" maxlength="200" placeholder="原因" value="@Model.Remarks">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                    @*<div class="form-group" style="text-align:center;margin-top:10px;">
                            <button type="submit" class="btn btn-blue boxShadowNone btnWidth120" id="btnSubmit">提交</button>
                        </div>*@
                    <div class="form-group" style="text-align:center;margin-top:10px;">
                        @if (ViewBag.IsCheckman && ViewBag.Resubmit)
                        {
                            foreach (WorkflowTransition item in ViewBag.ActionList)
                            {
                                <button type="button" class="btn btn-blue boxShadowNone btnWidth120 submit" value="@item.Code">@item.Name</button>
                            }
                        }
                        else
                        {
                            <button type="button" class="btn btn-blue boxShadowNone btnWidth120 submit">提交</button>
                        }

                    </div>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
            <div class="guidance">
                <div class="top-info">说明:</div>
                1.邮件地址由英文字母、数字及英文“.”号组成，不区分大小写<br />
                2.外协员工邮件地址由系统自动生成，不允许修改<br />
                3.对外显示名不允许包含特殊字符，如：@@符号,下划线，#号等<br />
            </div>
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</section>
<!-- /.content -->
<!-- cautionModal---start -->
<div id="cautionModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="addModal">
    <div class="modal-dialog modal-md" style="width:550px" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">注意事项</h4>
            </div>
            <div class="modal-body">
                <div style="text-align: center; color: Red; font-size: 14px;">
                    您的EMAIL地址更改后将会有以下影响：
                </div>
                1、EMAIL地址更改后，Email地址更改前所有通过RMS加密的Office文档您将无权限打开；<br />
                2、EMAIL地址更改后通过域帐号赋权的各项权限可能会失效，您需要重新申请授权。
                是否更改，请您谨慎选择！
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
            </div>
        </div>
    </div>
</div>
<!-- cautionModal---end -->

@section Scripts{
    <script src="~/js/plugins/autoComplete/jquery.autocomplete.js"></script>
    <script src="~/js/layer-extend.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //[AutoComplete]1、禁止输入逗号
            $(".userAC").keydown(function () {
                if (event.keyCode == 188) {
                    return false;
                }
            });

            //[AutoComplete]2、对域账号控件绑定自动完成功能
            if ($(".userAC").autocomplete) {
                var url = '@Url.Content("~/ACUser/GetUserCnsamList/")';
                $(".userAC").autocomplete(url, {
                    width: 300,
                    multiple: false,
                    mustMatch: true,
                    formatItem: getCnsamComplexstring,
                    formatResult: getSamFromComplexstring
                }).result(function (event, item) {
                    //showUserMessage();
                });
            }

            $("#cautionModal").modal('show');
        });

        //----------不使用视图组件处理三级部门start----------
        @*if ('@ViewBag.Resubmit'.toLowerCase() == 'false' && '@(SessionUtility.GetBasicUserInfo().ThirdDeptCode)' != "") {
            $("#FirstDeptCode").val('@(SessionUtility.GetBasicUserInfo().FirstDeptCode)');
            setSecondDept();
            $("#SecondDeptCode").val('@(SessionUtility.GetBasicUserInfo().SecondDeptCode)');
            setThirdDept();
            $("#ThirdDeptCode").val('@(SessionUtility.GetBasicUserInfo().ThirdDeptCode)');
        } else {
            $("#FirstDeptCode").val('@(Model.FirstDeptCode)');
            setSecondDept();
            $("#SecondDeptCode").val('@(Model.SecondDeptCode)');
            setThirdDept();
            $("#ThirdDeptCode").val('@(Model.ThirdDeptCode)');
        }*@

        //function setSecondDept() {
        //    $("#SecondDeptCode").empty();
        //    var dept1Code = $("#FirstDeptCode").val();
        //    $.ajax({
        //        url: "/Sys/Sys_Department/GetDept2",
        //        data: "dept1Code=" + dept1Code + "&hasExtra=false&isHistory=0",
        //        method: "get",
        //        async: false,
        //        dataType: "json",
        //        success: function (data) {
        //            $.each(data, function (i, item) {
        //                $("<option></option>")
        //                    .val(item["Dept2Code"])
        //                    .text(item["Dept2Name"])
        //                    .appendTo($("#SecondDeptCode"));
        //            });
        //            setThirdDept();
        //        },
        //        error: function (err) { }
        //    });
        //}

        //function setThirdDept() {
        //    $("#ThirdDeptCode").empty();
        //    var dept2Code = $("#SecondDeptCode").val();
        //    $.ajax({
        //        url: "/Sys/Sys_Department/GetDept3",
        //        data: "dept2Code=" + dept2Code + "&hasExtra=false&isHistory=0",
        //        method: "get",
        //        async: false,
        //        dataType: "json",
        //        success: function (data) {
        //            $.each(data, function (i, item) {
        //                $("<option></option>")
        //                    .val(item["Dept3Code"])
        //                    .text(item["Dept3Name"])
        //                    .appendTo($("#ThirdDeptCode"));
        //            });
        //            setManager();
        //        },
        //        error: function (err) { }
        //    });
        //}

        //function setManager() {
        //    var dept1Code = $("#FirstDeptCode").val();
        //    var dept2Code = $("#SecondDeptCode").val();
        //    var dept3Code = $("#ThirdDeptCode").val();
        //    if (dept1Code == "-1" || dept2Code == "-1" || dept3Code == "-1") {
        //        return;
        //    }
        //    $.ajax({
        //        url: "/Sys/Sys_Department/GetManagerAndId",
        //        data: "dept1Code=" + dept1Code + "&dept2Code=" + dept2Code + "&dept3Code=" + dept3Code,
        //        method: "get",
        //        async: false,
        //        dataType: "json",
        //        success: function (data) {
        //            $("#DeptManager").val(data.DeptManager);
        //            $("#DeptId").val(data.DeptId);
        //        },
        //        error: function (err) { }
        //    });
        //}
        //----------不使用视图组件处理三级部门end----------

        $(".submit").click(function () {
            var newEmailName = $("#NewEmailName").val();
            var simpleDisplayName = $("#SimpleDisplayName").val();
            var samAccountName = $("#SAMAccountName").val();
            var firstDeptCode = $("#FirstDeptCode").val();
            var secondDeptCode = $("#SecondDeptCode").val();
            var thirdDeptCode = $("#ThirdDeptCode").val();
            var deptName = $("#ThirdDeptCode").find("option:selected").text();
            var deptManager = $("#DeptManager").val();
            var remarks = $("#Remarks").val();
            var taskId = $("#TaskId").val();
            if (IsNullOrEmpty(samAccountName)) {
                alert("请输入域账号！");
                return;
            }
            var usertypePrefix = samAccountName.substring(1, 3);
            if (isWaixieType(usertypePrefix)) {
                alert("外协员工不允许修改邮件地址及对外显示名！");
                $("#SAMAccountName").val("");
                return;
            }
            if (IsNullOrEmpty(newEmailName)) {
                alert("新Email地址不能为空！");
                return;
            }
            if (IsNullOrEmpty(simpleDisplayName)) {
                alert("对外显示名不能为空！");
                return;
            }
            if (!IsEmailNoPostfix(simpleDisplayName)) {
                alert("对外显示名不能包含中文及特殊字符！");
                $("#SimpleDisplayName").focus();
                return;
            }
            if (IsNullOrEmpty(firstDeptCode)) {
                alert("请选择一级部门！");
                return;
            }
            if (IsNullOrEmpty(secondDeptCode)) {
                alert("请选择二级部门！");
                return;
            }
            if (IsNullOrEmpty(thirdDeptCode)) {
                alert("请选择三级部门！");
                return;
            }
            if (IsNullOrEmpty(deptManager)) {
                alert("部门经理不能为空！");
                return;
            }
            if (deptManager.split(" ").length != 2) {
                alert("部门经理数据格式错误！");
                return;
            }
            if (IsNullOrEmpty(remarks)) {
                alert("请输入原因！");
                return;
            }
            IndexBtnDisabled();
            $("#Action").val($(this).val());
            $("#DeptName").val(deptName);
            $.ajax({
                type: "post",
                url: "/PersonalAccount/PersonalAccount_AlterEmailAddress/Submit",
                data: $("#dataForm").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.Success) {
                        alert(data.Message);
                        $("#SAMAccountName").val('');
                        $("#OldEmailName").val('');
                        $("#NewEmailName").val('');
                        $("#SimpleDisplayName").val('');
                        $("#CreateDate").val(GetCurrentTime());
                        $("#Remarks").val('');
                        if (IsNullOrEmpty(taskId)) {
                            //插入成功后跳转到首页
                            activeTopTabByPageId("10008");
                            activeAndRefreshSubmitedListTab();
                        } else {
                            closeAndRefreshUnderCheckingList();
                        }
                    } else {
                        alert(data.Message);
                    }
                    IndexBtnEnabled();
                },
                error: function (err) { }
            });
        });

        function getEmailAddress(_this) {
            var $input = $(_this); //得到点击的文本框对象
            var samAccountName = $input.val();
            if (samAccountName == undefined || samAccountName.trim() == "" || samAccountName.length < 6) {
                return;
            }
            $.ajax({
                type: "get",
                url: "/Ajax/GetDomainAccountEmailAddress",
                data: "samAccountName=" + samAccountName,
                dataType: "json",
                success: function (data) {
                    if (data == "") {
                        $input.val('');
                        alert("该域账号不存在或未开通邮箱！");
                        $("#OldEmailName").val("");
                        $("#NewEmailName").val('');
                    } else {
                        $("#OldEmailName").val(data);
                        $("#NewEmailName").val('');
                    }
                },
                error: function (err) { }
            });
        }

        function checkEmailAddres() {
            var newEmailName = $("#NewEmailName").val();
            if (newEmailName == "") return;
            if (!IsEmailNoPostfix(newEmailName)) {
                alert("新邮件地址只能包含字母和 . 且不能以 . 结尾");
                $("#NewEmailName").val('');
                $("#NewEmailName").focus();
                return false;
            }
            var url = '@Url.Content("~/Ajax/CheckEmailAddress/")';
            $.getJSON(url, { emails: newEmailName, oldEmailAddress: "" }, function (data) {
                if (data.flag != 0) {
                    alert(data.Message);
                    if (data.emailAddress != undefined) {
                        $("#NewEmailName").val(data.emailAddress);
                    }
                    else {
                        $("#NewEmailName").val("");
                    }
                }
            });
        }
    </script>
    @await Component.InvokeAsync("ThirdLevelDept", new { firstDeptId = "FirstDeptCode", secondDeptId = "SecondDeptCode", thirdDeptId = "ThirdDeptCode", handleDeptManager = true, deptManagerId = "DeptManager", deptId = "DeptId" })
}